package com.jushu.storbox.service;

import java.util.List;

import android.content.Context;

import com.jushu.storbox.ShareDriveApplication;
import com.jushu.storbox.constant.ClientConfig;
import com.jushu.storbox.database.DAOFactory;
import com.jushu.storbox.database.ITeamSpaceDao;
import com.jushu.storbox.entities.FileFolderInfo;
import com.jushu.storbox.entities.TeamSpaceInfo;
import com.jushu.storbox.vo.PagedList;
import com.huawei.sharedrive.sdk.android.exception.ClientException;
import com.huawei.sharedrive.sdk.android.modelV2.request.CreateTeamSpaceRequest;
import com.huawei.sharedrive.sdk.android.modelV2.request.GetTeamSpaceRequest;
import com.huawei.sharedrive.sdk.android.modelV2.request.MyTeamSpaceRequest;
import com.huawei.sharedrive.sdk.android.modelV2.request.UpdataTeamSpaceRequest;
import com.huawei.sharedrive.sdk.android.modelV2.response.TeamSpaceEntity;
import com.huawei.sharedrive.sdk.android.modelV2.response.TeamSpaceMemberListResourse;
import com.huawei.sharedrive.sdk.android.serviceV2.TeamClient;

public class TeamSpaceService implements ITeamSpaceService {

	@Override
	public TeamSpaceInfo createTeamSpaceFromServer(Context context,
			CreateTeamSpaceRequest request) throws ClientException {
		// TODO Auto-generated method stub
		TeamSpaceEntity teamSpaceEntity=TeamClient.getInstance().createTeamSpace(ShareDriveApplication.getInstance().getAuthorization(), request);
		ITeamSpaceDao teamSpaceDao=DAOFactory.instance(context).getTeamSpaceDao();
		teamSpaceDao.insertTeamSpace(teamSpaceEntity,"1",ClientConfig.TEAM_SPACE_ADMIN);
		return teamSpaceDao.getTeamSpace(teamSpaceEntity.getId());
	}

	@Override
	public TeamSpaceInfo updateTeamSpaceFromServer(Context context,
			UpdataTeamSpaceRequest request) throws ClientException {
		// TODO Auto-generated method stub
		TeamSpaceEntity teamSpaceEntity=TeamClient.getInstance().updataTeamSpace(ShareDriveApplication.getInstance().getAuthorization(), request);
		ITeamSpaceDao teamSpaceDao=DAOFactory.instance(context).getTeamSpaceDao();
		teamSpaceDao.updateTeamSpace(teamSpaceEntity);
		return teamSpaceDao.getTeamSpace(teamSpaceEntity.getId());
	}

	@Override
	public String deleteTeamSpaceFromServer(Context context,
			GetTeamSpaceRequest request) throws ClientException {
		// TODO Auto-generated method stub
		String res=null;
		res=TeamClient.getInstance().deleteTeamSpace(ShareDriveApplication.getInstance().getAuthorization(), request);
		ITeamSpaceDao teamSpaceDao=DAOFactory.instance(context).getTeamSpaceDao();
		teamSpaceDao.deleteTeamSpace(request.getTeamId());
		return res;
	}

	@Override
	public TeamSpaceInfo getTeamSpaceMessageFromServer(Context context,
			GetTeamSpaceRequest request) throws ClientException {
		// TODO Auto-generated method stub
		TeamSpaceEntity teamSpaceEntity=TeamClient.getInstance().getTeamSpaceMessage(ShareDriveApplication.getInstance().getAuthorization(), request);
		ITeamSpaceDao teamSpaceDao=DAOFactory.instance(context).getTeamSpaceDao();
		teamSpaceDao.updateTeamSpace(teamSpaceEntity);
		return teamSpaceDao.getTeamSpace(teamSpaceEntity.getId());
	}

	@Override
	public TeamSpaceInfo getTeamSpaceMessageFromLocal(Context context,
			GetTeamSpaceRequest request) {
		// TODO Auto-generated method stub
		ITeamSpaceDao teamSpaceDao=DAOFactory.instance(context).getTeamSpaceDao();
		return teamSpaceDao.getTeamSpace(request.getTeamId());
	}

	@Override
	public PagedList<TeamSpaceInfo> getTeamSpaceListFromServer(Context context,
			MyTeamSpaceRequest request) throws ClientException {
		// TODO Auto-generated method stub
		PagedList<TeamSpaceInfo> pagedList = new PagedList<TeamSpaceInfo>();
		TeamSpaceMemberListResourse resourse=TeamClient.getInstance().getMyTeamSpacesList(ShareDriveApplication.getInstance().getAuthorization(), request);
		ITeamSpaceDao teamSpaceDao=DAOFactory.instance(context).getTeamSpaceDao();
		List<TeamSpaceInfo> clientList=teamSpaceDao.getTeamSpacesList();
		TeamSpaceHelper.compareServiceClientTeamSpace(context, resourse.getMemberships(), clientList);
		pagedList.setPagedList(teamSpaceDao.getTeamSpacesList());
		pagedList.setTotal(resourse.getTotalCount());
		return pagedList;
	}

	@Override
	public List<TeamSpaceInfo> getTeamSpaceListFromLocal(Context context) {
		// TODO Auto-generated method stub
		ITeamSpaceDao teamSpaceDao=DAOFactory.instance(context).getTeamSpaceDao();
		return teamSpaceDao.getTeamSpacesList();
	}

}
